/*
	Dijstra Shortest Path
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

const int MAXN = 100;
const int MAXM = 100;
const int INT_MAX = 65535;

typedef struct node {
	int matrix[MAXN][MAXM];
	int nVertex;
} MGraph;

void DijstraPath(MGraph G, int *dist, int *path, int source) {
	// 与source直接相连的赋值，其他MAX
	bool * visited = (bool *)malloc(sizeof(bool)*G.nVertex);
	for(int i=0; i<G.nVertex; i++) {
		if( i != source && G.matrix[source][i] > 0)
			dist[i] = G.matrix[source][i];
		else
			dist[i] = INT_MAX;
		visited[i] = false;
	}
	visited[source] = true;
	dist[source] = 0;

	for(int i=1; i<G.nVertex; i++) {
		int min_dist, current_vertex; // 到当前节点的最短路程
		min_dist = INT_MAX;
		for(int i=0; i<G.nVertex; i++) {
			if(!visited[i] && dist[i]<min_dist) {
				min_dist = dist[i];
				current_vertex = i;
			}
		}

		visited[current_vertex] = true;
		// min, vertex
		for(int i=0; i<G.nVertex; i++) {
			if(!visited[i] && G.matrix[current_vertex][i]>0 && min_dist+G.matrix[current_vertex][i]<dist[i]) {
				dist[i] = min_dist + G.matrix[current_vertex][i];
			}
		}
	}
}

int main(int argc, char * argv[]) {
	int Edges, Vertexes; // 顶点数和边数

	while(cin >> Vertexes >> Edges && Edges != 0) {
		MGraph G;
		G.nVertex = Vertexes;
		for(int i=0; i<Vertexes; i++) {
			for(int j=0; j<Vertexes; j++)
				G.matrix[i][j] = 0;
		}

		// 边 // 文件读取
		int u, v, w; // u->v 的路程w
		for(int i=0; i<Edges; i++) {
			cin >> u >> v >> w;
			G.matrix[u][v] = w;
		}

		int source, dest; // 
		cin >> source >> dest;

		int * dist = new int[Vertexes];
		int * path = new int[Vertexes];
		
		DijstraPath(G, dist, path, source);

		cout << dist[dest] << endl;

		delete []dist;
		delete []path;
	}

	return 0;
}
